#! /bin/bash
# generic revise script
# arguments: station, [ori|1h] [,year [,month]]

YEAR=2014
#YEAR=1976
#OPTIONS="-n"
OPTIONS="-v"
REVOPTIONS="-m-1"

export PYTHONPATH=$HOME/src/python/site-packages
BIN=$PYTHONPATH/nmdb/bin
SUFFIX=`date +%Y%m%d`
MINPARAMS=1

if [ $# -lt "$MINPARAMS" ]; then
  echo "This script needs at least $MINPARAMS command-line arguments (station (plus mode))!"
  exit
fi

if [ -n "$1" ]   # Tested variable is quoted.
then
  station=$1
fi
echo $station

if [ "$2" = "ori" ]; then
  MODE="ori"
  SQL="_1m.sqlite"
  SQLMODE="1m"
elif [ "$2" = "1h" ]; then
  MODE="1h"
  SQL="_1h.sqlite"
  SQLMODE="1h"
else
  MODE="none"
  echo "Unknown mode: please select ori or 1h"
  exit
fi

# If a third argument is given, use this as year
if [ -n "$3" ]   # Tested variable is quoted.
then
  YEAR=$3
fi

STATION="$(echo $station | tr '[:lower:]' '[:upper:]')"

# remove old sqlite file
rm /var/tmp/nmdb/$STATION$SQL

# If a fourth argument is given, use this as month
if [ -n "$4" ]   # Tested variable is quoted.
then
  MONTH=$4
  # convert one month to new sqlite file
  $BIN/station2sql.py $station $SQLMODE --year $YEAR --month $MONTH
else
# EITHER
  $BIN/station2sql.py $station $SQLMODE --year $YEAR
# OR
# read from existing yearly sqlite files
#ln -s /var/tmp/nmdb/$STATION$SQL.$YEAR /var/tmp/nmdb/$STATION$SQL
fi

# revise with new sqlite file, send unlimited data (-m-1)
$BIN/revise.py $station $MODE $REVOPTIONS
